
import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;


public class TestingGUI extends javax.swing.JFrame {
	public JPanel testingPanel1;
	public JButton bVibrationMotorOff;
	public JButton bBuzzerOn;
	public JTextField TBuzzer;
	public JButton BLEDOff;
	public JButton BLED;
	public JTextField TstatusLED;
	public JButton bVibrationMotorOn;
	public JButton bSavePort;
	public JTextField TportName;
	
	private SocWear socWear;

	/**
	* Auto-generated main method to display this JFrame
	*/
//	public static void main(String[] args) {
//		
//		TestingGUI inst = new TestingGUI();
//		inst.setLocationRelativeTo(null);
//		inst.setVisible(true);
//	}
	
	public TestingGUI() {
		super();
		
		socWear = new SocWear(new GUI(), "D:/msg.txt");
		
		initGUI();
		setListeners();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				testingPanel1 = new JPanel();
				getContentPane().add(testingPanel1, BorderLayout.CENTER);
				testingPanel1.setLayout(null);
				testingPanel1.setPreferredSize(new java.awt.Dimension(384, 219));
				{
					TportName = new JTextField();
					testingPanel1.add(TportName);
					TportName.setText("Please Input the Port Name Here");
					TportName.setBounds(40, 5, 207, 36);
				}
				{
					bSavePort = new JButton();
					testingPanel1.add(bSavePort);
					bSavePort.setText("Connect");
					bSavePort.setBounds(253, 11, 95, 24);
				}
				{
					bVibrationMotorOff = new JButton();
					testingPanel1.add(bVibrationMotorOff);
					bVibrationMotorOff.setText("Vibration Motor Off");
					bVibrationMotorOff.setBounds(202, 65, 141, 24);
				}
				{
					bVibrationMotorOn = new JButton();
					testingPanel1.add(bVibrationMotorOn);
					bVibrationMotorOn.setText("Vibration Motor On");
					bVibrationMotorOn.setBounds(40, 65, 151, 24);
				}
				{
					TstatusLED = new JTextField();
					testingPanel1.add(TstatusLED);
					TstatusLED.setText("Enter char to light on LED");
					TstatusLED.setBounds(20, 103, 166, 30);
				}
				{
					BLED = new JButton();
					testingPanel1.add(BLED);
					BLED.setText("LED On");
					BLED.setBounds(192, 106, 80, 24);
				}
				{
					TBuzzer = new JTextField();
					testingPanel1.add(TBuzzer);
					TBuzzer.setText("Enter a char to on the Buzzer");
					TBuzzer.setBounds(40, 149, 189, 36);
				}
				{
					bBuzzerOn = new JButton();
					testingPanel1.add(bBuzzerOn);
					bBuzzerOn.setText("Buzzer on");
					bBuzzerOn.setBounds(241, 155, 95, 24);
				}
				{
					BLEDOff = new JButton();
					testingPanel1.add(BLEDOff);
					BLEDOff.setText("LED Off");
					BLEDOff.setBounds(277, 106, 78, 24);
				}
			}
			pack();
			setSize(400, 300);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	
	private void setListeners() {
		
		// Connect button
		bSavePort.addMouseListener(new MouseListener() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				socWear.connectToiCATch(TportName.getText());
				TportName.setText("Connected to " + TportName.getText());
				bSavePort.setEnabled(false);
			}
			public void mouseEntered(MouseEvent arg0) {}
			public void mouseExited(MouseEvent arg0) {}
			public void mousePressed(MouseEvent arg0) {}
			public void mouseReleased(MouseEvent arg0) {}
		});
		
		
		BLED.addMouseListener(new MouseListener() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				((LED) socWear.dm.getDeviceByType(Device.LED)).on(TstatusLED.getText().charAt(0));
			}
			public void mouseEntered(MouseEvent arg0) {}
			public void mouseExited(MouseEvent arg0) {}
			public void mousePressed(MouseEvent arg0) {}
			public void mouseReleased(MouseEvent arg0) {}
		});
		
		
		BLEDOff.addMouseListener(new MouseListener() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				((LED) socWear.dm.getDeviceByType(Device.LED)).off(TstatusLED.getText().charAt(0));
			}
			public void mouseEntered(MouseEvent arg0) {}
			public void mouseExited(MouseEvent arg0) {}
			public void mousePressed(MouseEvent arg0) {}
			public void mouseReleased(MouseEvent arg0) {}
		});
		
		bVibrationMotorOn.addMouseListener(new MouseListener() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				((VibrationMotor) socWear.dm.getDeviceByType(Device.VIBRATION_MOTOR)).on();
			}
			public void mouseEntered(MouseEvent arg0) {}
			public void mouseExited(MouseEvent arg0) {}
			public void mousePressed(MouseEvent arg0) {}
			public void mouseReleased(MouseEvent arg0) {}
		});
		
		bVibrationMotorOff.addMouseListener(new MouseListener() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				((VibrationMotor) socWear.dm.getDeviceByType(Device.VIBRATION_MOTOR)).off();
			}
			public void mouseEntered(MouseEvent arg0) {}
			public void mouseExited(MouseEvent arg0) {}
			public void mousePressed(MouseEvent arg0) {}
			public void mouseReleased(MouseEvent arg0) {}
		});
		
		bBuzzerOn.addMouseListener(new MouseListener() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				((Buzzer) socWear.dm.getDeviceByType(Device.BUZZER)).playSound(TBuzzer.getText().charAt(0));
			}
			public void mouseEntered(MouseEvent arg0) {}
			public void mouseExited(MouseEvent arg0) {}
			public void mousePressed(MouseEvent arg0) {}
			public void mouseReleased(MouseEvent arg0) {}
		});
		
	}

}
